#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion


using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using IPSim.GUI.Devices;

namespace IPSim.GUI
{
    public partial class DeviceConsole : UserControl
    {
        #region

        private IConsoleAccess attachedDevice = null;

        #endregion

        #region Methods

        public DeviceConsole()
        {
            InitializeComponent();
        }

        public void WriteText(string text)
        {
            consoleTextBox1.AddText(text);
        }

        public void SetPrompt(string newprompt)
        {
            consoleTextBox1.Prompt = newprompt;
        }

        public void TryToAttachDevice(BaseDevice device)
        {
            if (attachedDevice != null)
                attachedDevice.DeattachConsole();

            if (device is IConsoleAccess)
            {
                attachedDevice = (IConsoleAccess)device;
                attachedDevice.AttachConsole(this);
                consoleTextBox1.Clear();
                WriteText(attachedDevice.GetDeviceProlog());                
                consoleTextBox1.DisplayPrompt();

            }
        }

        #endregion

        #region Properties

        [Category("Appereance")]
        [Browsable(true)]
        [Description("Console Text Color")]
        public Color ConsoleTextColor
        {
            get { return consoleTextBox1 != null ? consoleTextBox1.ForeColor : Color.Green; }
            set
            {
                if (consoleTextBox1 != null)
                    consoleTextBox1.ForeColor = value;
            }
        }

        [Category("Appereance")]
        [Browsable(true)]
        [Description("Console Background Color")]
        public Color ConsoleBackgroundColor
        {
            get { return consoleTextBox1 != null ? consoleTextBox1.BackColor : Color.Black; }
            set
            {
                if (consoleTextBox1 != null)
                    consoleTextBox1.BackColor = value;
            }
        }

        [Category("Appereance")]
        [Browsable(true)]
        [Description("Console Text Font")]
        public Font ConsoleTextFont
        {
            get { return consoleTextBox1 != null ? consoleTextBox1.Font : new Font("Tahoma", 8); }
            set
            {
                if (consoleTextBox1 != null)
                    consoleTextBox1.Font = value;
            }
        }

        internal IConsoleAccess DeviceAttached
        {
            get
            {
                return attachedDevice;
            }
        }

        #endregion

    }

    #region CommandHistory class

    internal class CommandHistory
    {
        private int currentPosn;
        private string lastCommand;
        private ArrayList commandHistory = new ArrayList();

        internal CommandHistory()
        {
        }

        internal void Add(string command)
        {
            if (command != lastCommand)
            {
                commandHistory.Add(command);
                lastCommand = command;
                currentPosn = commandHistory.Count;
            }
        }

        internal bool DoesPreviousCommandExist()
        {
            return currentPosn > 0;
        }

        internal bool DoesNextCommandExist()
        {
            return currentPosn < commandHistory.Count - 1;
        }

        internal string GetPreviousCommand()
        {
            lastCommand = (string)commandHistory[--currentPosn];
            return lastCommand;
        }

        internal string GetNextCommand()
        {
            lastCommand = (string)commandHistory[++currentPosn];
            return LastCommand;
        }

        internal string LastCommand
        {
            get { return lastCommand; }
        }

        internal string[] GetCommandHistory()
        {
            return (string[])commandHistory.ToArray(typeof(string));
        }
    }

    #endregion
}
